<?php

namespace app\manage\model;

use app\common\loader\ModelSystem;
use app\common\model\system\SystemManagerModel as CommonSystemManagerModel;
use app\manage\loader\ManageServices;
use think\Exception;

class SystemMangerModel extends CommonSystemManagerModel
{


    /**
     *  用户登录
     * @param string $username
     * @param string $password
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws Exception
     */
    public function login(string $username, string $password)
    {
        $userInfo = $this->db()->where('username|mobile', $username)->find();
        if (empty($userInfo) || $userInfo[$this->fieldStatus] === 0) {
            throw  new Exception('用户不存在或已禁用', API_ERROR);
        }
        if ($userInfo[$this->fieldPasswd] !== md5(md5($password))) {
            throw  new Exception('用户密码不正确', API_ERROR);
        }
        $this->loginAfter($userInfo[$this->fieldId]);
        return $userInfo;
    }

    /**
     * 登录后处理
     * @param int $userid
     * @return void
     */
    protected function loginAfter(int $userid): void
    {

    }


    /**
     *  检测登录的信息是否正确, 正确返回用户信息,失败返回空,错误信息放到msg中,
     * @param array $logininfo 要检测的登录信息
     * @param string $msg 接收返回的内容
     * @return array 返回的用户信息
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function validUserInfo(array $logininfo, &$msg): array
    {
        if (empty($logininfo["id"])) {
            $msg = '用户ID不能为空';
            return [];
        }
        $adminInfo = $this->where($this->fieldId, $logininfo['id'])->where("status", 1)
            ->cache(true, MINUTES_SECOND)->field("passwd,id,is_super,nickname,mobile,username,role_id")->find();
        if (empty($adminInfo)) {
            $msg = '用户不存在或已被禁用';
            return [];
        }

        if ($logininfo["password"] !== md5($adminInfo["passwd"])) {
            $msg = '密码错误,请重新登录。。';
            return [];
        }
        $msg = "";
        return $adminInfo;
    }

    /**
     * 管理员列表
     * @param array $where
     * @param int $page_size
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function pageList(array $where, int $page_size = 30)
    {
        return $this->db()->where($where)
            ->field('*,role_id as role_name,dept_id as dept_name')
            ->filter(function ($user){
                $user['role_name'] =   $this->db('system_roles')->where('id',$user['role_id'])->value('title') ?? '暂未分配';
                $user['dept_name'] = $this->db('system_department')->where('id',$user['dept_id'])->value('title') ?? '暂未分配';
                return $user;
            },'id')
            ->paginate($page_size);
    }


    /**
     * 管理员列表
     * @param array $where
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function adminList(array $where)
    {
        return $this->db()->where($where)
            ->field('*,role_id as role_name,dept_id as dept_name')
            ->filter(function ($user) {
                $user['role_name'] = $this->db('system_roles')->where('id', $user['role_id'])->value('title') ?? '暂未分配';
                $user['dept_name'] = $this->db('system_department')->where('id', $user['dept_id'])->value('title') ?? '暂未分配';
                return $user;
            }, 'id')
            ->order('id desc')
            ->select();
    }

    /**
     * 管理员详情
     * @param array $where
     * @param int $page_size
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function info(array $option)
    {
        return $this->db()->where($option['where'])
            ->field('id,username,mobile,nickname,status,memo,create_at,dept_id,dept_id as dept_name,is_super,role_id,role_id as role_name')
            ->withAttr([
                'role_name' => function ($v){return  $this->db('system_roles')->where('id',$v)->value('title') ?? '';},
                'dept_name' => function ($v){return  $this->db('system_department')->where('id',$v)->value('title') ?? '';}
            ])
            ->find();
    }
}